In this notebook, we compare performance of two stocks against their revenue and attempt to discern a correlation.
!pip install yfinance
!pip install pandas
!pip install requests
!pip install plotly
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: yfinance in /home/lenixc/.local/lib/python3.10/site-packages (0.1.67) Requirement already satisfied: requests>=2.20 in /home/lenixc/.local/lib/python3.10/site-packages (from yfinance) (2.26.0) Requirement already satisfied: lxml>=4.5.1 in /home/lenixc/.local/lib/python3.10/site-packages (from yfinance) (4.6.4) Requirement already satisfied: multitasking>=0.0.7 in /home/lenixc/.local/lib/python3.10/site-packages (from yfinance) (0.0.10) Requirement already satisfied: numpy>=1.15 in /usr/lib/python3.10/site-packages (from yfinance) (1.22.3) Requirement already satisfied: pandas>=0.24 in /home/lenixc/.local/lib/python3.10/site-packages (from yfinance) (1.3.3) Requirement already satisfied: pytz>=2017.3 in /home/lenixc/.local/lib/python3.10/site-packages (from pandas>=0.24->yfinance) (2022.1) Requirement already satisfied: python-dateutil>=2.7.3 in /home/lenixc/.local/lib/python3.10/site-packages (from pandas>=0.24->yfinance) (2.8.2) Requirement already satisfied: six>=1.5 in /usr/lib/python3.10/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance) (1.16.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3.10/site-packages (from requests>=2.20->yfinance) (2021.10.8) Requirement already satisfied: charset-normalizer~=2.0.0 in /home/lenixc/.local/lib/python3.10/site-packages (from requests>=2.20->yfinance) (2.0.12) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3.10/site-packages (from requests>=2.20->yfinance) (1.26.8) Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3.10/site-packages (from requests>=2.20->yfinance) (3.3) Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: pandas in /home/lenixc/.local/lib/python3.10/site-packages (1.3.3) Requirement already satisfied: numpy>=1.17.3 in /usr/lib/python3.10/site-packages (from pandas) (1.22.3) Requirement already satisfied: python-dateutil>=2.7.3 in /home/lenixc/.local/lib/python3.10/site-packages (from pandas) (2.8.2) Requirement already satisfied: pytz>=2017.3 in /home/lenixc/.local/lib/python3.10/site-packages (from pandas) (2022.1) Requirement already satisfied: six>=1.5 in /usr/lib/python3.10/site-packages (from python-dateutil>=2.7.3->pandas) (1.16.0) Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: requests in /home/lenixc/.local/lib/python3.10/site-packages (2.26.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3.10/site-packages (from requests) (2021.10.8) Requirement already satisfied: charset-normalizer~=2.0.0 in /home/lenixc/.local/lib/python3.10/site-packages (from requests) (2.0.12) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/lib/python3.10/site-packages (from requests) (1.26.8) Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3.10/site-packages (from requests) (3.3) Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: plotly in /home/lenixc/.local/lib/python3.10/site-packages (5.3.1) Requirement already satisfied: tenacity>=6.2.0 in /home/lenixc/.local/lib/python3.10/site-packages (from plotly) (8.0.1) Requirement already satisfied: six in /usr/lib/python3.10/site-packages (from plotly) (1.16.0)
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
tesla = yf.Ticker("TSLA")
tesla_data = tesla.history(period="max")
tesla_data.reset_index(inplace=True)
tesla_data.tail()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 2992 | 2022-05-17 | 747.359985 | 764.479980 | 728.849976 | 761.609985 | 26745400 | 0 | 0.0 |
| 2993 | 2022-05-18 | 744.520020 | 760.500000 | 700.809998 | 709.809998 | 29270600 | 0 | 0.0 |
| 2994 | 2022-05-19 | 707.000000 | 734.000000 | 694.109985 | 709.419983 | 30098900 | 0 | 0.0 |
| 2995 | 2022-05-20 | 713.989990 | 721.580017 | 633.000000 | 663.900024 | 48190600 | 0 | 0.0 |
| 2996 | 2022-05-23 | 655.020020 | 679.960022 | 638.059998 | 674.900024 | 29380300 | 0 | 0.0 |
tesla_revenue = pd.read_html("https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue")[1]
tesla_revenue = tesla_revenue.rename(columns={"Tesla Quarterly Revenue(Millions of US $)": "Date", "Tesla Quarterly Revenue(Millions of US $).1": "Revenue"})
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
tesla_revenue.head()
/tmp/ipykernel_116818/1086719813.py:3: FutureWarning: The default value of regex will change from True to False in a future version.
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")
| Date | Revenue | |
|---|---|---|
| 0 | 2022-03-31 | 18756 |
| 1 | 2021-12-31 | 17719 |
| 2 | 2021-09-30 | 13757 |
| 3 | 2021-06-30 | 11958 |
| 4 | 2021-03-31 | 10389 |
gstop = yf.Ticker("GME")
gme_data = gstop.history(period="max")
gme_data.reset_index(inplace=True)
gme_data.tail()
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 5100 | 2022-05-17 | 93.900002 | 100.379997 | 91.199997 | 100.379997 | 2611200 | 0.0 | 0.0 |
| 5101 | 2022-05-18 | 96.379997 | 99.839996 | 90.169998 | 91.480003 | 2890000 | 0.0 | 0.0 |
| 5102 | 2022-05-19 | 91.220001 | 101.980003 | 90.160004 | 99.190002 | 3180100 | 0.0 | 0.0 |
| 5103 | 2022-05-20 | 99.809998 | 102.410004 | 89.750000 | 95.660004 | 2488100 | 0.0 | 0.0 |
| 5104 | 2022-05-23 | 96.570000 | 97.279999 | 91.500000 | 96.129997 | 1745200 | 0.0 | 0.0 |
gme_revenue = pd.read_html("https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue")[1]
gme_revenue = gme_revenue.rename(columns={"GameStop Quarterly Revenue(Millions of US $)": "Date", "GameStop Quarterly Revenue(Millions of US $).1": "Revenue"})
gme_revenue["Revenue"] = gme_revenue['Revenue'].str.replace(',|\$',"")
gme_revenue.head()
/tmp/ipykernel_116818/2802099722.py:3: FutureWarning: The default value of regex will change from True to False in a future version.
| Date | Revenue | |
|---|---|---|
| 0 | 2022-01-31 | 2254 |
| 1 | 2021-10-31 | 1297 |
| 2 | 2021-07-31 | 1183 |
| 3 | 2021-04-30 | 1277 |
| 4 | 2021-01-31 | 2122 |
make_graph(tesla_data, tesla_revenue, 'Tesla')
make_graph(gme_data, gme_revenue, 'GameStop')
While the Tesla stock price is loosely correlated with revenue, the climb started in early 2020 is not representative of the stocks history and is strange behavior not tied to the gradual rise of the revenue.
For GameStop, we notice that while the company has oscillating revenue, we don't see that oscillation in the valuation of the stock. Recently, the stock has seen a massive uptick in value, all the while GameStop's revenue is falling. This implies something outside of market forces is increasing the value of gamestop shares.
This project was made with help and under the instruction of the IBM Python For Data Science MOOC.